//
//  YJImagePickerController.m
//  CToolBox
//
//  Created by crj on 16/9/7.
//  Copyright © 2016年 oyf. All rights reserved.
//

#import "YJImagePickerController.h"
#import "HXCutPictureViewController.h"


typedef void(^didFinish)(UIImage *image);

@interface YJImagePickerController()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

@property (nonatomic, assign, readonly) UIViewController *controller;
@property (nonatomic, assign) UIImagePickerControllerSourceType *sourceType;
@property (nonatomic, copy) didFinish didFinish;

//相册或相机打开前 状态栏颜色状态
@property (nonatomic, assign)UIStatusBarStyle lastStatusBarStyle;


@property (nonatomic, assign) CGSize tempSize;
@property (nonatomic, assign) BOOL allowsEditing;
@end

@implementation YJImagePickerController

- (instancetype)initWithController:(UIViewController *)controller
{
    self = [super init];
    if (self) {
        _controller = controller;
        _lastStatusBarStyle =  [UIApplication sharedApplication].statusBarStyle;

    }
    return self;
}

- (void)openWithAllowsEditing:(BOOL)allowsEditing size:(CGSize)size didFinish:(void(^)(UIImage *image))didFinish{
    
    _allowsEditing = allowsEditing;
    _tempSize = size;
    
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",@"拍照", nil];
    [sheet showInView:[UIApplication sharedApplication].keyWindow];
    
    _didFinish = didFinish;
}

- (void)openWithAllowsEditing:(BOOL)allowsEditing
                         size:(CGSize)size
                   pickerType:(YJImagePickerType)type
                    didFinish:(void(^)(UIImage *image))didFinish{

    _allowsEditing = allowsEditing;
    _tempSize = size;
    _didFinish = didFinish;

    
    if(type == YJImagePickerTypeLibrary){
        [self openLibrary];
    }else{
        [self openCamera];
    }
}


- (void)showImagePicker{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    });
    [_controller presentViewController:self.imagePicker animated:YES completion:nil];
    
}

- (void)closeImagePicker{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication] setStatusBarStyle:_lastStatusBarStyle];
        });
    
        [self.imagePicker dismissViewControllerAnimated:YES completion:nil];

    });
    
}


#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    WeakSelf;
    UIImage *img = info[UIImagePickerControllerOriginalImage];
    if(self.allowsEditing){
        HXCutPictureViewController *vc = [[HXCutPictureViewController alloc] initWithCropImage:img  cropSize:_tempSize title:@"裁剪" isLast:YES];
        vc.completion = ^(HXCutPictureViewController *vc, UIImage *finishImage) {
            
            if(weakSelf.didFinish){
                
                weakSelf.didFinish(finishImage);
                
            }
            [weakSelf closeImagePicker];
            
        };
        
        [weakSelf.imagePicker presentViewController:vc animated:YES completion:nil];
    }else{
        if(weakSelf.didFinish){
           
            weakSelf.didFinish(img);
            
        }
        [weakSelf closeImagePicker];

    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    if(_didFinish){
        _didFinish(nil);
    }
    [self closeImagePicker];
}


#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    
    if(buttonIndex == 0){

        [self openLibrary];
    }else if(buttonIndex == 1){
        
        [self openCamera];
    }else if(buttonIndex == 2){//取消
        return;
    }
}

- (void)openLibrary{
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self showImagePicker];

}

- (void)openCamera{
    if(TARGET_IPHONE_SIMULATOR){
        [Factory alertMessage:@"模拟器下，相机不可用！" present:_controller];
        return;
    }
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self showImagePicker];

}

#pragma mark - GET

- (UIImagePickerController *)imagePicker{
    
    if(!_imagePicker){
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
        _imagePicker.modalPresentationStyle  = UIModalPresentationCustom;

    }
    return _imagePicker;
}

@end
